/**
 * JRadioButtonMenuItem.java
 */
package ca.uvic.cs.cogz.mapping.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JRadioButtonMenuItem;

/**
 * Extends {@link JRadioButtonMenuItem} to allow an icon on the checkbox (regardless off the Look &
 * Feel).
 * 
 * @author Chris Callendar
 * @date 2008-04-14
 */
public class JIconRadioButtonMenuItem extends JRadioButtonMenuItem {

	private JLabel label;

	public JIconRadioButtonMenuItem() {
		super();
	}

	public JIconRadioButtonMenuItem(Icon icon) {
		super();
		initialize(null, icon);
	}

	public JIconRadioButtonMenuItem(String text) {
		super();
		initialize(text, null);
	}

	public JIconRadioButtonMenuItem(Action a) {
		super(a);
		initialize((String) a.getValue(Action.NAME), (Icon) a.getValue(Action.SMALL_ICON), (String) a.getValue(Action.SHORT_DESCRIPTION));
	}

	public JIconRadioButtonMenuItem(Icon icon, boolean selected) {
		super(null, null, selected);
		initialize(null, icon);
	}

	public JIconRadioButtonMenuItem(String text, boolean selected) {
		super(null, null, selected);
		initialize(text, null);
	}

	public JIconRadioButtonMenuItem(String text, Icon icon) {
		super();
		initialize(text, icon);
	}

	public JIconRadioButtonMenuItem(String text, Icon icon, boolean selected) {
		super(null, null, selected);
		initialize(text, icon);
	}

	protected void initialize(String text, Icon icon) {
		initialize(text, icon, null);
	}

	protected void initialize(String text, Icon icon, String tooltip) {
		if ((text != null) && (icon != null)) {
			add(getLabelComponent());
			if (text != null) {
				getLabelComponent().setText(text);
			}
			if (icon != null) {
				getLabelComponent().setIcon(icon);
				int width = this.getFontMetrics(this.getFont()).stringWidth(text);
				width += 30 + icon.getIconWidth();
				setPreferredSize(new Dimension(width, Math.max(20, icon.getIconHeight() + 8)));
				label.setPreferredSize(this.getPreferredSize());
			}
			if (tooltip != null) {
				getLabelComponent().setToolTipText(tooltip);
			}
		}
	}

	public JLabel getLabelComponent() {
		if (label == null) {
			label = new JLabel();
			label.setOpaque(false);
			label.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
		}
		return label;
	}

	@Override
	public void setCursor(Cursor cursor) {
		super.setCursor(cursor);
		getLabelComponent().setCursor(cursor);
	}

	@Override
	public void setForeground(Color fg) {
		super.setForeground(fg);
		getLabelComponent().setForeground(fg);
	}

	@Override
	public void setFont(Font font) {
		super.setFont(font);
		getLabelComponent().setFont(font);

	}

	@Override
	public void setAction(Action a) {
		super.setAction(a);
		initialize((String) a.getValue(Action.NAME), (Icon) a.getValue(Action.SMALL_ICON), (String) a.getValue(Action.SHORT_DESCRIPTION));
	}

	@Override
	public void setIconTextGap(int iconTextGap) {
		super.setIconTextGap(iconTextGap);
		getLabelComponent().setIconTextGap(iconTextGap);
	}

	@Override
	public void setHorizontalTextPosition(int textPosition) {
		super.setHorizontalTextPosition(textPosition);
		getLabelComponent().setHorizontalTextPosition(textPosition);
	}

	@Override
	public void setText(String text) {
		getLabelComponent().setText(text);
	}

	@Override
	public void setIcon(Icon icon) {
		getLabelComponent().setIcon(icon);
	}

	@Override
	public void setVerticalTextPosition(int textPosition) {
		super.setVerticalTextPosition(textPosition);
		getLabelComponent().setVerticalTextPosition(textPosition);
	}

	@Override
	public void setEnabled(boolean b) {
		super.setEnabled(b);
		getLabelComponent().setEnabled(b);
	}

}
